package com.zou.mall.product.controller;

import com.zou.common.utils.PageUtils;
import com.zou.common.utils.R;
import com.zou.common.valid.AddGroup;
import com.zou.mall.product.entity.BrandEntity;
import com.zou.mall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 品牌
 *
 * @author lingjin.zou
 * @email leonard_zou@163.com
 * @date 2022-02-14 14:32:42
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    /**
     * 7. JSR03 给Bean添加校验注解@Valid 开启校验功能
     *  *  * 给bean的字段添加@NotBlank 设置校验属性
     *  *  * BindResult用来绑定@Valid来监听校验结果
     *  *  * com.zou.mall.product.controller.BrandController#save
     *  *  *
     *  *  * 分组校验
     *  *  * 标注不同情况需要进行校验
     */
    public R save(@Valid @RequestBody BrandEntity brand) {
        brandService.save(brand);
        return R.ok();
    }

    @RequestMapping("/save1")
    //@RequiresPermissions("product:brand:save")
    public R save1(@Validated(value = {AddGroup.class})  @RequestBody BrandEntity brand) {
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
